/*
Copyright (c)2006-2008 - Brett Lajzer

See LICENSE for license information.
*/

#include <esdl/SDL.h>
#include <esdl/SDL_image.h>
#include <esdl/SDL_video.h>
#include <lua.h>
#include <lauxlib.h>

#include "globals.h"
#include "funcs_timer.h"
#include "hashmap.h"
static last_t = 0; 
//function for loading a surface from many different formats
int l_getimage(lua_State *L){
	char fullImageFile[1024];
	SDL_Surface *surf;
	const char* imgageFile = luaL_checkstring(L,1);
   
  sprintf(fullImageFile,"%s/%s",gamefullpath,imgageFile);
  surf = (SDL_Surface *)hmap_search(sample_cache,fullImageFile);
  //std::map<std::string, SDL_Surface *>::iterator im = image_store.find(filename);

  //search for the image in the store, if not found, load it
  if(!surf){
    surf = IMG_Load(fullImageFile);
    
    if(surf == NULL){
    	return luaL_error(L, "ERROR: Can't find image file: \"%s\".\n%s", imgageFile, SDL_GetError());
    }
    
    hmap_insert(sample_cache,fullImageFile,-1,surf);
    
    lua_pushlightuserdata(L, surf);
    lua_pushinteger(L, (lua_Integer)surf->w);
    lua_pushinteger(L, (lua_Integer)surf->h);
    
    //SDL_FreeSurface(surf);
  }else{
    lua_pushlightuserdata(L, surf);
    lua_pushinteger(L, (lua_Integer)surf->w);
    lua_pushinteger(L, (lua_Integer)surf->h);
  }
  return 3;
}

//forces an image to be unloaded from memory
int l_releaseimage(lua_State *L){
  const char* imgageFile = luaL_checkstring(L,1);
  SDL_Surface *surf = (SDL_Surface *)hmap_search(sample_cache,imgageFile);

  if(!surf){
	//glDeleteTextures(1, &(im->second->texture));
    SDL_FreeSurface(surf);
	//delete surf;
	hmap_insert(sample_cache,imgageFile,-1,NULL);
  }

  return 0;
}


//function for updating the display surface
int l_flip(lua_State *L){
  //SDL_Flip(screen);
  step();
  SDL_BlitSurface(g_surface,NULL,screen,NULL);
  SDL_UpdateRect(screen,0,0,clientRect.w,clientRect.h);
  return 0;
}

//function for simple displaying of images (non-animated)
int l_display(lua_State *L){
  SDL_Surface *tex = (SDL_Surface *)lua_touserdata(L,1);
  SDL_Rect rect;
  float x = (float)lua_tonumber(L,2);
  float y = (float)lua_tonumber(L,3);
  double rot = lua_tonumber(L,4);
  double scale_x = lua_tonumber(L,5);
  double scale_y = lua_tonumber(L,6);
  float alpha = lua_tonumber(L, 7)/255.0;
  float w_half,h_half;
  
  if(tex == NULL){
  	return luaL_error(L, "ERROR: Can't draw. Image is null.");
  }
  
  w_half = tex->w/2.0;
  h_half = tex->h/2.0;
  
  rect.x=(Sint16)x;
  rect.y=(Sint16)y;
  rect.w = tex->w;
  rect.h = tex->h;
  SDL_BlitSurface(tex,NULL,g_surface,&rect);
  
  return 0;      
}


//function for displaying a frame of an animation
//frames are laid out horizontally
//frames start at 0
int l_displayframe(lua_State *L){
  SDL_Surface *tex = (SDL_Surface*)lua_touserdata(L,1);
  float x = (float)lua_tonumber(L,2);
  float y = (float)lua_tonumber(L,3);
  int numframes = lua_tointeger(L,4);
  int frame = lua_tointeger(L,5);
  double rot = lua_tonumber(L,6);
  double scale_x = lua_tonumber(L,7);
  double scale_y = lua_tonumber(L,8);
  float alpha = lua_tonumber(L, 9)/255.0;
  int width,frame_offset;
  float w_half,h_half; 
  if(tex == NULL){
	return luaL_error(L, "ERROR: Can't draw. Image is null.");
  }
  
  width = tex->w/numframes;
  frame_offset = frame * width;
  
  w_half = width/2.0;
  h_half = tex->h/2.0;
  
  
  
  return 0;      
}


//cursor toggler
int l_show_cursor(lua_State *L){
  BOOL show = lua_toboolean(L, 1);
  
  SDL_ShowCursor((show ? SDL_ENABLE : SDL_DISABLE));
       
  return 0;       
}


//fills screen with a color
int l_fill_screen(lua_State *L){
  
  return 0;      
}


//free surface interface
int l_delete_image(lua_State *L){
  SDL_Surface *tmp = (SDL_Surface*)lua_touserdata(L,1);

  if(tmp == NULL){
  	return luaL_error(L, "ERROR: Can't free. Image is null.");
  }
  //glDeleteTextures(1, &(tmp->texture));
  SDL_FreeSurface(tmp);
  lua_pushnil(L);
  return 1;
}

